package org.jeecg.modules.gyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gyy.gyyuser.entity.GyyUser;
import org.jeecg.modules.gyy.gyyuser.mapper.GyyUserMapper;
import org.jeecg.modules.gyy.mapper.PcUserMapper;
import org.jeecg.modules.gyy.service.PcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class PcUserServiceImpl extends ServiceImpl<GyyUserMapper, GyyUser> implements PcUserService {

    @Autowired(required = false)
    private PcUserMapper pcUserMapper;

    @Override
    public GyyUser getUserByName(String username) {
        return this.getOne(new LambdaQueryWrapper<GyyUser>().eq(GyyUser::getUsername, username));
    }

    @Override
    public GyyUser getUserByPhone(String phone) {
        return this.getOne(new LambdaQueryWrapper<GyyUser>().eq(GyyUser::getPhone, phone));
    }

    @Override
    public GyyUser getUserByEmail(String email) {
        return this.getOne(new LambdaQueryWrapper<GyyUser>().eq(GyyUser::getEmail, email));
    }

    @Override
    @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
    public Result<?> resetPassword(String username, String oldpassword, String newpassword, String confirmpassword) {
        GyyUser user = pcUserMapper.getUserByName(username);
        String passwordEncode = PasswordUtil.encrypt(username, oldpassword, user.getSalt());
        if (!user.getPassword().equals(passwordEncode)) {
            return Result.error("旧密码输入错误!");
        }
        if (oConvertUtils.isEmpty(newpassword)) {
            return Result.error("新密码不允许为空!");
        }
        if (!newpassword.equals(confirmpassword)) {
            return Result.error("两次输入密码不一致!");
        }
        String password = PasswordUtil.encrypt(username, newpassword, user.getSalt());
        this.pcUserMapper.update(new GyyUser().setPassword(password), new LambdaQueryWrapper<GyyUser>().eq(GyyUser::getId, user.getId()));
        return Result.ok("密码重置成功!");
    }

    @Override
    @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
    public Result<?> forgetPassword(String username, String newpassword, String confirmpassword) {
        GyyUser user = pcUserMapper.getUserByName(username);
        if (oConvertUtils.isEmpty(newpassword)) {
            return Result.error("新密码不允许为空!");
        }
        if (!newpassword.equals(confirmpassword)) {
            return Result.error("两次输入密码不一致!");
        }
        String password = PasswordUtil.encrypt(username, newpassword, user.getSalt());
        this.pcUserMapper.update(new GyyUser().setPassword(password), new LambdaQueryWrapper<GyyUser>().eq(GyyUser::getId, user.getId()));
        return Result.ok("密码重置成功!");
    }
}
